package me.pinion.handler;

import com.jfinal.handler.Handler;
import me.pinion.model.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


public class AccountHandler extends Handler {
    @Override
    public void handle(String target, HttpServletRequest request, HttpServletResponse response, boolean[] isHandled) {
        HttpSession session = request.getSession();
        User user = (User)session.getAttribute("user");
        if(user == null){
            session.setAttribute("user", new User());
        }else {
            User curUser = User.dao.findById(user.getId());
            session.setAttribute("user", curUser == null ? user : curUser);
        }

        nextHandler.handle(target, request, response, isHandled);
        User.dao.put((User)session.getAttribute("user"));
    }
}
